<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<!-- 引入toastr.css -->
<link rel="stylesheet" href="<?=$staticAdminUrl?>assets/toastr/toastr.min.css">
<style type="text/css">
 .upload-div{
	margin-top: 10px
 }
.reselect {
    font-size: 12px;
    border-radius: 10px;
    vertical-align: bottom;
	padding: 2px 12px;
    margin-top: 10px;
}
#other-format {
	font-size: 16px;
	margin-bottom: 8px;
}
#video {
    max-height: 500px;
}
#source-form img {
    max-height: 400px;
}
</style>
<div class="page-header">
    <h1>
        <?php if(isset($source['id']) && $source['id']): ?>编辑<?php else: ?>上传<?php endif ?><?= transform_source_format($format) ?>资源
    </h1>
</div>
<div class="col-xs-12">
    <form id="source-form" class="form-horizontal">
		<input type="hidden" name="format" value="<?= $format ?>" />
        <input type="hidden" name="id" value="<?= isset($source['id']) ? $source['id'] : '' ?>" />
        <input type="hidden" name="grade" value="<?= isset($source['source_grade_id']) ? $source['source_grade_id'] : '' ?>" />
        <!-- 资源封面提交输入框 -->
        <input type="hidden" name="cover" value='<?= isset($source['cover']) ? $source['cover'] : '' ?>' />
        <!-- 视频提交输入框 -->
		<input type="hidden" name="video" value='<?= isset($source['link']) ? $source['link'] : '' ?>' />
		<!-- 音频提交输入框 -->
		<input type="hidden" name="audio" value='<?= isset($source['link']) ? $source['link'] : '' ?>' />
		<!-- 其他格式文件提交输入框 -->
		<input type="hidden" name="other-format" value='<?= isset($source['link']) ? $source['link'] : '' ?>' />
        <div class="form-group">
            <label class="col-sm-3 control-label no-padding-right">资源标题：</label>
            <div class="col-sm-6">
                <input class="form-control" type="text" name="title" value="<?= isset($source['title']) ? $source['title'] : '' ?>" placeholder="请填写资源标题" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label no-padding-right">资源封面：</label>
            <div class="col-sm-6 upload-div">
                <?php if(isset($source['cover'])): ?>
					<div href="javascript:;" id="upload-img">
						<img src="<?= json_decode($source['cover'], true)['link'] ?>" />
                    </div>
                    <div id="reselect-cover" class="btn btn-info reselect">重新选择</div> 
                <?php else: ?>
					<div href="javascript:;" id="upload-img">
                        <img src="<?=$staticAdminUrl?>assets/images/upload.png" />
                    </div>
                    <div id="reselect-cover" class="btn btn-info reselect" style="visibility:hidden;">重新选择</div>          
                <?php endif; ?>
            </div>
		</div>
		<?php if($format === SOURCE_FORMAT_VIDEO): ?>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">视频文件：</label>
				<div class="col-sm-6 upload-div">
					<?php if(isset($source['link'])): ?>
						<video controls="controls" id="video" src="<?= json_decode($source['link'], true)['link'] ?>"></video>
                        <br/>
                        <div id="upload-video">
                            <div id="reselect-video" class="btn btn-info reselect">重新选择</div>
                        </div> 
                    <?php else: ?>
						<video controls="controls" id="video" src="" style="display: none;"></video>
						<div href="javascript:;" id="upload-video">
							<img src="<?=$staticAdminUrl?>assets/images/upload.png" />
							<div id="reselect-video" class="btn btn-info reselect" style="display:none;">重新选择</div>
						</div>
					<?php endif; ?>
				</div>
			</div>
		<?php endif ?>
		<?php if($format === SOURCE_FORMAT_AUDIO): ?>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">音频文件：</label>
				<div class="col-sm-6 upload-div">
					<?php if(isset($source['link'])): ?>
                        <audio id="audio" src="<?= json_decode($source['link'], true)['link'] ?>" controls="controls"></audio>
                        <br/>
                        <div id="upload-audio">
                            <div id="reselect-audio" class="btn btn-info reselect">重新选择</div>
                        </div>
					<?php else: ?>
						<audio controls="controls" id="audio" src="" controls="controls" style="display: none;"></audio>
						<div href="javascript:;" id="upload-audio">
							<img src="<?=$staticAdminUrl?>assets/images/upload.png" />
							<div id="reselect-audio" class="btn btn-info reselect" style="display:none;">重新选择</div>
						</div>
					<?php endif; ?>
				</div>
			</div>
		<?php endif ?>
		<?php if($format === SOURCE_FORMAT_OTHER): ?>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">上传文件：</label>
				<div class="col-sm-6 upload-div">
                    <?php if(isset($source['link'])): ?>
                        <?php $url = (strstr($source['link'], '.zip') || strstr($source['link'], '.rar') || strstr($source['link'], '.gz') || strstr($source['link'], '.z')) ? json_decode($source['link'], true)['link'] : admin_url('source/source_preview') . '?source_src='. json_decode($source['link'], true)['link']; ?>
						<a href="<?= $source['linkPreview'] ?>" id="check-file-a" target="_blank">
							<div id="other-format">
                                <i class="ace-icon fa fa-film bigger-110"></i>
                                &nbsp;
                                <span id="file-name"><?= json_decode($source['link'], true)['name'] ?></span>
                            </div>
                        </a>
                        <div id="upload-other-format">
                            <div id="reselect-other-format" class="btn btn-info reselect">重新选择</div>
                        </div>
					<?php else: ?>
						<div id="other-format" style="display: none">
							<a href="javascript:;" id="check-file-a" target="_blank">
								<i class="ace-icon fa fa-film bigger-110"></i>
								&nbsp;
								<span id="file-name"></span>
							</a>
						</div>
						<div href="javascript:;" id="upload-other-format">
							<img src="<?=$staticAdminUrl?>assets/images/upload.png" />
							<div id="reselect-other-format" class="btn btn-info reselect" style="display:none;">重新选择</div>
						</div>
					<?php endif; ?>
				</div>
			</div>
		<?php endif ?>
		<div class="form-group">
            <label class="col-sm-3 control-label no-padding-right">所属年级：</label>
            <div class="col-sm-6">
                <span class="block input-icon input-icon-right pd0">
                    <input class="width-100" type="text" placeholder="请选择年级" id="select-source-grade" value="<?= isset($source['grade_name']) ? $source['grade_name'] : '' ?>" />
                    <i class="ace-icon fa fa-graduation-cap"></i>
                </span>
            </div>
		</div>
		<div class="form-group">
            <label class="col-sm-3 control-label no-padding-right">所属科目：</label>
            <div class="col-sm-6">
				<select class="form-control" name="subject">
					<?php foreach($subjects as $subject): ?>
                        <option value="<?= $subject['id'] ?>" <?php if(isset($source['source_subject_id']) && $subject['id'] == $source['source_subject_id']): ?>selected<?php endif ?>>
                            <?= $subject['title'] ?>
                        </option>
					<?php endforeach ?> 
				</select>
            </div>
		</div>
		<div class="form-group">
            <label class="col-sm-3 control-label no-padding-right">资源类型：</label>
            <div class="col-sm-6">
				<select class="form-control" name="type">
					<?php foreach($types as $type): ?>
                        <option value="<?= $type['id'] ?>" <?php if(isset($source['source_type_id']) && $type['id'] == $source['source_type_id']): ?>selected<?php endif ?>>
                            <?= $type['title'] ?>
                        </option>
					<?php endforeach ?> 
				</select>
            </div>
		</div>
		<div class="form-group">
            <label class="col-sm-3 control-label no-padding-right">教材版本：</label>
            <div class="col-sm-6">
				<select class="form-control" name="publisher">
					<?php foreach($publishers as $publisher): ?>
                        <option value="<?= $publisher['id'] ?>" <?php if(isset($source['source_publisher_id']) && $publisher['id'] == $source['source_publisher_id']): ?>selected<?php endif ?>>
                            <?= $publisher['title'] ?>
                        </option>
					<?php endforeach ?> 
				</select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label no-padding-right">资源详情：</label>
            <div class="col-sm-6">
                <textarea class="pd0" id="content" name="desc" style="height: 200px;width: 100%;<?php if($format === SOURCE_FORMAT_IMAGE_TEXT): ?>height: 400px;<?php endif ?>"><?= isset($source['desc']) ? $source['desc'] : '' ?>
				</textarea>
            </div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right">资源共享：</label>
            <div class="col-sm-6">
				<select class="form-control" name="is-shared">
                    <option value="1"
                        <?php if(isset($source['is_shared']) && $source['is_shared'] == 1): ?>
                            selected
                        <?php endif ?>
                    >
                        是
                    </option>
                    <option value="0"
                        <?php if(isset($source['is_shared']) && $source['is_shared'] == 0): ?>
                            selected
                        <?php endif ?>
                    >
                        否
                    </option>
				</select>
            </div>
		</div>
        <div class="clearfix">
            <div class="col-md-offset-3 col-md-9">
                <button class="btn btn-info" type="submit">
                    <i class="ace-icon fa fa-check"></i> 提交
                </button>
            </div>
        </div>
	</form>
</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<!-- 引入toastr.js,基于jquery.js，故得在footer.php前面引用-->
<script src="<?=$staticAdminUrl?>/assets/toastr/toastr.min.js"></script>
<!-- 百度编辑器 -->
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.config.js"></script>
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.all.js"></script>
<script type="text/javascript">
$(function() {
    // 初始化编辑器
    var ue = UE.getEditor('content');

    // 初始化选择资源年级控件
    $("#select-source-grade").selectSourceGrade({
        "modalDataUrl": '<?= admin_url('source/select_source_grade_plugin_data_source') ?>',
        "resultDom": $("[name=grade]"),
	});

    var upFromurl = '<?=UP_FORM_URL?>';
    var signature = '<?=$signature?>';
    var policy = '<?=$policy?>';

    // 上传封面
    $("#upload-img").uploader({
        "server": upFromurl,
        "authorization": signature,
        "policy": policy,
        "callback": function(response) {
			// // 如果已上传过封面，且是新建资源的情况，删除又拍云该图片
			// if (!$("[name=id]").val() && $("[name=cover]").val()) {
            //     var postUrl = "<?=admin_url('source/delete_upyun_file')?>";
            //     file = JSON.parse($("[name=cover]").val())
            //     var postData = {
            //         "url": file.link
            //     };
            //     ajax_post(postUrl, postData);
            // }
            var fileName = decodeURIComponent(response.name);
            var fileLink = upyunUrl + response.url;
            $("#upload-img").find("img").attr("src", fileLink);
            var file = {
                "name": fileName,
                "link": fileLink
            };
            $("[name=cover]").val(JSON.stringify(file));
            // 显示重新选择按钮
            reselectCover = $('#reselect-cover');
            if (reselectCover) {
                reselectCover.show();
                reselectCover.css('visibility', 'visible');
            }
        }
    });

    // 重新选择封面
    $("#reselect-cover").uploader({
        "server": upFromurl,
        "authorization": signature,
        "policy": policy,
        "callback": function(response) {
            var fileName = decodeURIComponent(response.name);
            var fileLink = upyunUrl + response.url;
            $("#upload-img").find("img").attr("src", fileLink);
            var file = {
                "name": fileName,
                "link": fileLink
            };
            $("[name=cover]").val(JSON.stringify(file));
        }
    });

    // 上传视频
    $("#upload-video").uploader({
        "server": upFromurl,
        "authorization": signature,
        "policy": policy,
        "accept": "<?= SOURCE_FORMAT_VIDEO ?>",
        "callback": function(response) {
            layer.load();
			// // 如果已上传过视频，且是新建资源的情况，删除又拍云该视频
			// if (!$("[name=id]").val() && $("[name=video]").val()) {
            //     var postUrl = "<?=admin_url('source/delete_upyun_file')?>";
            //     file = JSON.parse($("[name=video]").val())
            //     var postData = {
            //         "url": file.link
            //     };
            //     ajax_post(postUrl, postData);
            // }
            // 添加附件
            var fileName = decodeURIComponent(response.name);
            var fileLink = upyunUrl + response.url;
            // 先隐藏添加按钮
            $img = $("#upload-video img");
            if ($img) {
                $img.hide();
            }
            // 显示重新选择按钮
            $reselectVideo = $('#reselect-video');
            if ($reselectVideo) {
                $reselectVideo.show();
            }
            // 显示video
            $("#video").show();
            $("#video").attr("src", fileLink);
            var file = {
                "name": fileName,
                "link": fileLink
            };
            $("[name=video]").val(JSON.stringify(file));

            layer.closeAll('loading');
        }
	});

    // 上传音频
    $("#upload-audio").uploader({
        "server": upFromurl,
        "authorization": signature,
        "policy": policy,
        "accept": "<?= SOURCE_FORMAT_AUDIO ?>",
        "callback": function(response) {
            // // 如果已上传过音频，且是新建资源的情况，删除又拍云该视频
            // if (!$("[name=id]").val() && $("[name=audio]").val()) {
            //     var postUrl = "<?=admin_url('source/delete_upyun_file')?>";
            //     file = JSON.parse($("[name=audio]").val())
            //     var postData = {
            //         "url": file.link
            //     };
            //     ajax_post(postUrl, postData);
            // }
            // 添加附件
            var fileName = decodeURIComponent(response.name);
            var fileLink = upyunUrl + response.url;
            // 先隐藏添加按钮
            $img = $("#upload-audio img");
            if ($img) {
                $img.hide();
            }
            // 显示重新选择按钮
            $reselectAudio = $('#reselect-audio');
            if ($reselectAudio) {
                $reselectAudio.show();
            }
            // 显示audio
            $("#audio").show();
            $("#audio").attr("src", fileLink);
            var file = {
                "name": fileName,
                "link": fileLink
            };
            $("[name=audio]").val(JSON.stringify(file));
        }
	});

	// 上传其他格式文件
	$("#upload-other-format").uploader({
        "server": upFromurl,
        "authorization": signature,
        "policy": policy,
        "accept": "<?= SOURCE_FORMAT_OTHER ?>",
        "callback": function(response) {
            // // 如果已上传过文件，且是新建资源的情况，删除又拍云该文件
            // if (!$("[name=id]").val() && $("[name=other-format]").val()) {
            //     var postUrl = "<?=admin_url('source/delete_upyun_file')?>";
            //     file = JSON.parse($("[name=other-format]").val())
            //     var postData = {
            //         "url": file.link
            //     };
            //     ajax_post(postUrl, postData);
            // }
            // 添加附件
            var fileName = decodeURIComponent(response.name);
            var fileLink = upyunUrl + response.url;
            // 先隐藏添加按钮
            $img = $("#upload-other-format img");
            if ($img) {
                $img.hide();
            }
            // 显示重新选择按钮
            $reselectOtherFormat = $('#reselect-other-format');
            if ($reselectOtherFormat) {
                $reselectOtherFormat.show();
			}
            // 显示other-format
            $("#other-format").show();
            $("#file-name").text(fileName);
            // if(fileLink.indexOf('.zip') !== -1 || fileLink.indexOf('.rar') !== -1 || fileLink.indexOf('.gz') !== -1 || fileLink.indexOf('.z') !== -1){
            //     previewUrl = fileLink;
            // } else {
            //     previewUrl = "<?= admin_url('source/source_preview') ?>" + '?source_src=' + fileLink;
            // }
            // office文件支持预览
            if(fileLink.search(/^.*\.(doc|docx|ppt|pptx|xls|xlsx)$/) !== -1) {
                previewUrl = "https://view.officeapps.live.com/op/view.aspx?src=" + fileLink;
            } else {
                previewUrl = fileLink;
            }
			$("#check-file-a").attr('href', previewUrl);
            var file = {
                "name": fileName,
                "link": fileLink
            };
            $("[name=other-format]").val(JSON.stringify(file));
        }
	});
	
	// 提交表单
	var form = $("#source-form");
	var submitUrl = "<?=admin_url('source/save_source_action')?>";
	form_submit(form, submitUrl, function(res) {
		tips_alert(res.msg, res.success, function(){
			if(res.success) {
				window.location.href = "<?=admin_url('source/manage')?>";
			}
		});
	})
})
</script>